package com.ruoyi.web.controller.prevention;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.PreventionSafetyPromise;
import com.ruoyi.system.entity.PreventionSafetyPromiseFile;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("preventionSafetyPromise")
public class PreventionSafetyPromiseController extends BaseController {

    @Resource
    private PreventionSafetyPromiseService preventionSafetyPromiseService;
    @Resource
    private PreventionSafetyPromiseFileService preventionSafetyPromiseFileService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysDeptService iSysDeptService;


    /**
     * 分页查询
     * @param preventionSafetyPromise 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(PreventionSafetyPromise preventionSafetyPromise) {
        startPage();
        List<PreventionSafetyPromise> preventionSafetyPromiseList = this.preventionSafetyPromiseService.queryByPage(preventionSafetyPromise);
        if (!CollectionUtils.isEmpty(preventionSafetyPromiseList)) {
            for (PreventionSafetyPromise promise : preventionSafetyPromiseList) {
                List<PreventionSafetyPromiseFile> promiseFiles = new ArrayList<>();
                String fileId = promise.getSafetyFileId();
                if (StringUtils.isNotEmpty(fileId)) {
                    fileId = fileId.substring(0, fileId.length() - 1);
                    if (fileId.contains(",")) {
                        String[] split = fileId.split(",");
                        List<String> id = Arrays.asList(split);
                        if (StringUtils.isNotEmpty(id)){
                            for (String s : id) {
                                PreventionSafetyPromiseFile promiseFile = preventionSafetyPromiseFileService.queryById(Integer.valueOf(s));
                                if (!ObjectUtils.isEmpty(promiseFile)){
                                    promiseFiles.add(promiseFile);
                                }
                            }
                        }
                    } else {
                        PreventionSafetyPromiseFile promiseFile = preventionSafetyPromiseFileService.queryById(Integer.valueOf(fileId));
                        if (!ObjectUtils.isEmpty(promiseFile)){
                            promiseFiles.add(promiseFile);
                        }
                    }
                }
                promise.setSafetyPromiseFileList(promiseFiles);
            }
        }
        return getDataTable(preventionSafetyPromiseList);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.preventionSafetyPromiseService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param preventionSafetyPromise 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody PreventionSafetyPromise preventionSafetyPromise) {
        return AjaxResult.success(this.preventionSafetyPromiseService.insert(preventionSafetyPromise));
    }

    /**
     * 编辑数据
     *
     * @param preventionSafetyPromise 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody PreventionSafetyPromise preventionSafetyPromise) {
        return AjaxResult.success(this.preventionSafetyPromiseService.update(preventionSafetyPromise));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.preventionSafetyPromiseService.deleteById(id));
    }

    /**
     * 文件上传
     * @return 上传结果
     */
    @PostMapping("/fileUpload")
    public AjaxResult uploadAcceptance(@RequestBody PreventionSafetyPromise preventionSafetyPromise) {
        if (null == preventionSafetyPromise) {
            return AjaxResult.error("参数异常！");
        }
        if (null == preventionSafetyPromise.getId()) {
            return AjaxResult.error("参数异常！");
        }
        if (CollectionUtils.isEmpty(preventionSafetyPromise.getSafetyPromiseFileList())) {
            return AjaxResult.error("参数异常！");
        }
        StringBuilder key = new StringBuilder();
        if (!CollectionUtils.isEmpty(preventionSafetyPromise.getSafetyPromiseFileList())) {
            for (PreventionSafetyPromiseFile promiseFile : preventionSafetyPromise.getSafetyPromiseFileList()) {
                preventionSafetyPromiseFileService.insert(promiseFile);
                key.append(promiseFile.getId());
                key.append(",");
            }
        }
        if (!StringUtils.isEmpty(key.toString())) {
            PreventionSafetyPromise promise = new PreventionSafetyPromise();
            promise.setId(preventionSafetyPromise.getId());
            promise.setSafetyFileId(key.toString());
            preventionSafetyPromiseService.update(promise);
        }
        return AjaxResult.success();
    }



    @Log(title = "安全承诺卡(", businessType = BusinessType.EXPORT)
    @PostMapping("/exportData")
    public void export(HttpServletResponse response, @RequestBody Integer[] ids) {
        List<PreventionSafetyPromise> promiseList = new ArrayList<>();
        List<Integer> list = new ArrayList<>(Arrays.asList(ids));
        if (!CollectionUtils.isEmpty(list)) {
            for (Integer id : list) {
                PreventionSafetyPromise promise = preventionSafetyPromiseService.queryById(id);
                promiseList.add(promise);
            }
        }
        /*if (!CollectionUtils.isEmpty(promiseList)) {
            promiseList.forEach(val -> {
               if (null != val.getTaskState()) {
                    if (val.getTaskState() == 0) {
                        val.setTaskStateBase("待处理");
                    }
                    if (val.getTaskState() == 1) {
                        val.setTaskStateBase("已处理");
                    }
                }
                if (StringUtils.isNotEmpty(val.getSafetyAcceptorId())) {
                    SysUser sysUser = iSysUserService.selectUserById(Long.valueOf(val.getSafetyAcceptorId()));
                    if (null != sysUser) {
                        val.setNickName(sysUser.getNickName());
                        if (null != sysUser.getDeptId()) {
                            SysDept sysDept = iSysDeptService.selectDeptById(sysUser.getDeptId());
                            if (null != sysDept) {
                                val.setDeptName(sysDept.getDeptName());
                            }
                        }
                    }
                }
            });
        }*/
        ExcelUtil<PreventionSafetyPromise> util = new ExcelUtil<>(PreventionSafetyPromise.class);
        util.exportExcel(response, promiseList, "安全承诺卡(");
    }


    @GetMapping("export/pdf/{id}")
    public void exportPdf(@PathVariable("id") Integer id, HttpServletRequest request, HttpServletResponse response) {
        preventionSafetyPromiseService.fillPDFData(id, request, response);
    }












}

